import html2canvas from "html2canvas";
import jspdf from "jspdf";


export const InfoData = [
    {
        id: 1,
        land_node_id: 3701051012050000002,
        land_area: 2.95,
        name: '张中华',
        country_name: '天桥区',
        isBasicLand: '是',
        land_name: '东坡',
        children: [
            {
                id: 11,
                land_node_id: 3701051012050000002,
                land_area: 3.11,
                name: '张中华',
                country_name: '天桥区',
                isBasicLand: '是',
                land_name: '东坡',
            },
        ],
    },
    {
        id: 2,
        land_node_id: 3701051012050000002,
        land_area: 2.95,
        name: '张中华',
        country_name: '天桥区',
        isBasicLand: '是',
        land_name: '东坡',
    },
    {
        id: 3,
        land_node_id: 3701051012050000002,
        land_area: 2.95,
        name: '刘鑫',
        country_name: '天桥区',
        isBasicLand: '是',
        land_name: '东坡',
    },
    {
        id: 3,
        land_node_id: 3701051012050000002,
        land_area: 2.95,
        name: '甘尼',
        country_name: '天桥区',
        isBasicLand: '是',
        land_name: '东坡',
    },
    {
        id: 3,
        land_node_id: 3701051012050000002,
        land_area: 2.95,
        name: '王德法',
        country_name: '天桥区',
        isBasicLand: '是',
        land_name: '东坡',
    },
    {
        id: 3,
        land_node_id: 3701051012050000002,
        land_area: 2.95,
        name: '普陀',
        country_name: '天桥区',
        isBasicLand: '是',
        land_name: '东坡',
    },
    {
        id: 3,
        land_node_id: 3701051012050000002,
        land_area: 2.95,
        name: '刘仁义',
        country_name: '天桥区',
        isBasicLand: '是',
        land_name: '东坡',
    },
    {
        id: 3,
        land_node_id: 3701051012050000002,
        land_area: 2.95,
        name: '曲林烟',
        country_name: '天桥区',
        isBasicLand: '是',
        land_name: '东坡',
    }
];



export function exportPdf(element: HTMLElement, title) {
    html2canvas(element, {
        logging: false     //一开始canvas设置不渲染页面
    }).then((canvas) => {
        let pdf: any = new jspdf('p', 'mm', 'a4') //纵向，单位mm，A4纸张大小
        let ctx: any = canvas.getContext('2d'); //预设2维画布
        let a4w: number = 190;  //设置显示内容的大小，A4大小：210*297；最后显示在A4内部区域大小为：190*360
        let a4h: number = 260;
        let imgHeight: number = Math.floor(a4h * canvas.width / a4w); //按A4显示比例换算一页图像的像素高度
        let renderedHeight: number = 0;

        while (renderedHeight < canvas.height) {//判断页面有内容时
            let page: any = document.createElement('canvas'); //创建画布
            page.width = canvas.width; //设置画布宽高等于内容宽高
            page.height = Math.min(imgHeight, canvas.height - renderedHeight); //画布的高等于内容的最小的高度（不足一页）
            //用getImageData裁剪指定区域，并绘制到前面创建的canvas对象中
            let a: any = page.getContext('2d');
            a.putImageData(ctx.getImageData(0, renderedHeight, canvas.width, Math.min(imgHeight, canvas.height - renderedHeight)), 0, 0);
            pdf.addImage(page.toDataURL('image/jpeg', 1.0), 'JPEG', 10, 10, a4w, Math.min(a4h, a4w * page.height / page.width)); //添加图片到页面，保留10mm边距

            renderedHeight += imgHeight;
            if (renderedHeight + imgHeight < canvas.height) {
                pdf.addPage();
            }

        }
        title = title ? title : 'content';
        pdf.save(title +  '.pdf')
    });
}